﻿using System;
using System.Collections.ObjectModel;
using System.Windows;
using StockholmsStad.Windows.UI.Modules.Maintenance.Interfaces;
using StockholmsStad.Windows.UI.Modules.Maintenance.Models;

namespace StockholmsStad.Windows.UI.Modules.Maintenance.Controls
{
    /// <summary>
    /// Interaction logic for TabItemFilingView.xaml
    /// </summary>
    public partial class FilingListView : IFilingListView
    {
        readonly IMaintenanceService maintenanceService;

        public FilingListView(IMaintenanceService service)
        {
            maintenanceService = service;
            InitializeComponent();
        }

        #region IFilingView Members

        public ObservableCollection<DepersonateCouple> Model
        {
            get { return DataContext as ObservableCollection<DepersonateCouple>; }
            set { DataContext = value; }
        }

        #endregion

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            var warning = string.Empty;
            if (!dtpDateFrom.SelectedDate.HasValue && !dtpDateTo.SelectedDate.HasValue)
            {
                warning = "Ange Från och Till datum!";
            }
            else if (!dtpDateFrom.SelectedDate.HasValue)
            {
                warning = "Ange Från datum!";
            }
            else if (!dtpDateTo.SelectedDate.HasValue)
            {
                warning = "Ange Till datum!";
            }

            if (string.IsNullOrEmpty(warning))
            {
                var fromDate = (DateTime) (dtpDateFrom.SelectedDate.HasValue ? dtpDateFrom.SelectedDate : DateTime.MinValue);
                var toDate = (DateTime) (dtpDateTo.SelectedDate.HasValue ? dtpDateTo.SelectedDate : DateTime.MaxValue);

                Model = maintenanceService.GetCouplesForDepersonation(fromDate, toDate);

                NoOfCouples.Text = "Antal par: " + Model.Count;

                if (Model.Count == 0)
                {
                    MessageBox.Show("Inga par hittades!", "Information", MessageBoxButton.OK, MessageBoxImage.Information);                    
                }
            }
            else
            {
                MessageBox.Show(warning, "Information", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }

        private void btnDepersonate_Click(object sender, RoutedEventArgs e)
        {
            //Check if at least one couple is selected
            if (lvCouples.SelectedItems != null && lvCouples.SelectedItems.Count > 0)
            {
                if (MessageBox.Show("Vill du verkligen arkivera de markerade paren?", "Information",
                                    MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                {
                    var ret = maintenanceService.DepersonateCouples(lvCouples.SelectedItems);

                    MessageBox.Show("Arkiverade " + ret + " par!", "Information", MessageBoxButton.OK, MessageBoxImage.Information);

                    btnSearch_Click(sender, e);
                }
            }
            else
            {
                MessageBox.Show("Du måste markera minst ett par i listan. ", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }

        private void OnSelectAllChanged(object sender, RoutedEventArgs e)
        {
            if (cbSelectAll.IsChecked.HasValue && cbSelectAll.IsChecked.Value)
                lvCouples.SelectAll();
            else
            {
                lvCouples.UnselectAll();
            }
        }

        private void OnUncheckItem(object sender, RoutedEventArgs e)
        {
            cbSelectAll.IsChecked = false;
        }

        private void lvCouples_SourceUpdated(object sender, System.Windows.Data.DataTransferEventArgs e)
        {
            var test = lvCouples.Items.Count;
        }
    }
}
